import _ from 'lodash';

type ParseType = {
  id: string[] | number[];
  nodeType: string[];
};
/**
 * 解析字符串 匹配id和deviceType
 * @param arr
 * @param needUniqType
 */
export const parseArray = (arr: string[], needUniqType = false): ParseType => {
  let result = {
    id: [],
    nodeType: [],
  };
  if (Array.isArray(arr)) {
    arr?.forEach((item) => {
      const idMatch = item.match(/id(\d*)/);
      const nodeTypeMatch = item.match(/nodeType(.+)/);
      if (idMatch && idMatch[1] !== '') {
        result.id.push(idMatch[1]);
      }
      if (nodeTypeMatch) {
        result.nodeType.push(nodeTypeMatch[1]);
      }
    });
    return {
      id: result.id,
      nodeType: !needUniqType ? _.uniq(result.nodeType) : result.nodeType,
    };
  } else {
    const idMatch = arr?.match(/id(\d*)/);
    const nodeTypeMatch = arr?.match(/nodeType(.+)/);
    if (idMatch && idMatch[1] !== '') {
      result.id.push(idMatch[1]);
    }
    if (nodeTypeMatch) {
      result.nodeType.push(nodeTypeMatch[1]);
    }
    return {
      id: result.id,
      nodeType: !needUniqType ? _.uniq(result.nodeType) : result.nodeType,
    };
  }
};
/**
 * 获得分区列表
 * @param arr
 */
export const getFilterList = (ansObj: any) => {
  let areaList = [];
  let codeList = [];
  ansObj.id.forEach((item, index) => {
    if (ansObj.nodeType[index] === 'AREA') {
      areaList.push(item);
    } else {
      codeList.push(item);
    }
  });
  return {
    areaList: areaList,
    codeList: codeList,
  };
};
